package com.hzlj.position.locate.differentiation.common;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fz.common.base.core.AddressResolutionUtil;
import com.fz.common.base.dto.AddressDTO;
import com.google.common.collect.Maps;
import com.hzlj.common.message.common.dto.message.MessageNoticeResultDTO;
import com.hzlj.common.message.common.util.MessageRestUtil;
import com.hzlj.position.config.common.enums.WarningType;
import com.hzlj.position.config.service.NoticeConfigService;
import com.hzlj.position.locate.common.model.Position;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class PositionWarningNoticeJzry {
    @Resource
    private NoticeConfigService noticeConfigService;

    public Map<String, MessageNoticeResultDTO> notice(List<Position> positions) {
        //给矫正人员发送通知(立即)
        Map<String, MessageNoticeResultDTO> notices = Maps.newHashMap();
        for (Position position : positions) {
            if (!noticeConfigService.warningJzryNotice(position.getJgCode(), position.getWarningType())) {
                continue;
            }
            try {
                String messageTemplate = "position-warning-" + position.getWarningType();
                Map<String, Object> params = Maps.newHashMap();
                params.put("warningTypeText", WarningType.of(position.getWarningType()).getText());
                params.put("positionTime", DateUtil.formatDateTime(position.getPositionTime()));
                //位置信息
                if (ObjectUtil.isNotEmpty(position.getPosition())) {
                    AddressDTO address = AddressResolutionUtil.addressResolution(position.getPosition());
                    if (address != null) {
                        params.put("positionFmt", address.getProvince() + address.getCity() + address.getCounty());
                    }
                }
                MessageNoticeResultDTO notice = MessageRestUtil.noticeQueryUsers(
                        messageTemplate,
                        position.getId(),
                        params,
                        false,
                        position.getJzId()
                );
                notices.put(position.getId(), notice);
            } catch (Exception e) {
                log.error("发送定位告警消息失败[通知矫正人员]报错:{}", position, e);
            }
        }
        return notices;
    }
}
